﻿namespace Pathfinding
{
    using System;

    public class MinHeap
    {
        private SearchNode listHead;

        public void Add(SearchNode item)
        {
            if (this.listHead == null)
            {
                this.listHead = item;
            }
            else if ((this.listHead.next == null) && (item.cost <= this.listHead.cost))
            {
                item.nextListElem = this.listHead;
                this.listHead = item;
            }
            else
            {
                SearchNode listHead = this.listHead;
                while ((listHead.nextListElem != null) && (listHead.nextListElem.cost < item.cost))
                {
                    listHead = listHead.nextListElem;
                }
                item.nextListElem = listHead.nextListElem;
                listHead.nextListElem = item;
            }
        }

        public SearchNode ExtractFirst()
        {
            SearchNode listHead = this.listHead;
            this.listHead = this.listHead.nextListElem;
            return listHead;
        }

        public bool HasNext()
        {
            return (this.listHead != null);
        }
    }
}

